package com.link.base.base.approval.controller;

import com.link.base.base.approval.model.FlowRecord;
import com.link.base.base.approval.service.FlowRecordService;
import com.link.core.basic.annotations.JsonParam;
import com.link.core.basic.controller.BasicController;
import com.link.core.basic.dao.KeyGenerateDao;
import com.link.core.basic.model.BasicModel;
import com.link.core.basic.query.Filter;
import com.link.core.basic.query.QueryParams;
import com.link.core.basic.query.Sorter;
import com.link.core.basic.service.BasicService;
import com.link.core.cllog.LCLogger;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import java.text.SimpleDateFormat;
import java.util.*;

/**
 * Copyright  hande Company. All Rights Reserved.
 *
 * @author unknwon
 * @version 1.0
 * @date: 2018/11/8 14:35
 */

@Controller
@RequestMapping("/link/flowRecord")
public class FlowRecordController extends BasicController<FlowRecord> {
    @Resource
    private FlowRecordService flowRecordService;

    @Resource
    private KeyGenerateDao keyGenerateDao;

    @Override
    public BasicService<FlowRecord> getBasicService() {
        return flowRecordService;
    }


    @RequestMapping(value = "/insert", method = RequestMethod.GET, produces = "application/json")
    @ResponseBody
    public Map<String, Object> insert(@JsonParam FlowRecord record, HttpServletRequest request, HttpServletResponse response) {
        Map<String, Object> result = new HashMap<>(4);
        try {
            record.setId(keyGenerateDao.keyGenerate());
            Date now = new Date();
            SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm");
            // new Date()为获取当前系统时间
            String approvalEnd = dateFormat.format(now);
            record.setApprovalEnd(approvalEnd);
            flowRecordService.flowRecordInsert(record);
            result.put("success", true);
        } catch (Exception e) {
            LCLogger.withException(e);
            result.put("success", false);
            result.put("result", e.getMessage());
        }
        return result;
    }

    /**
     * 查询审批记录
     */
    @RequestMapping(value = "/listApproRecord", method = RequestMethod.POST)
    @ResponseBody
    public Map<String, Object> listApproRecord(@JsonParam QueryParams qps, HttpSession session,
                                               HttpServletRequest request, HttpServletResponse response) {
        Map<String, Object> result = new HashMap<>(4);
        try {
            qps.invokePreFilterAndSorter();
            List<Filter> filterList = qps.getFilterByProperty("headId");
            Long headId = 0L;
            if (null == qps.getId()) {
                if (filterList != null && filterList.size() > 0) {
                    Filter filter = filterList.get(0);
                    headId = Long.parseLong(filter.getValue());
                }
            } else {
                headId = qps.getId();
            }
            Long noSelect = 0L;
            if (noSelect.equals(headId)) {
                //按创建时间来排序
                Sorter sorter = new Sorter("created", Sorter.DIR_DESC);
                qps.addSorter(sorter);
                qps.setAttr1(headId.toString());
                FlowRecord entity = new FlowRecord();
                entity.setAttr1(headId.toString());
                List<FlowRecord> listFlowRecord = flowRecordService.queryObjFlowRecordListPage(entity);
                result.put("success", true);
                result.put("rows", listFlowRecord);
                result.put("total", getCount(entity, listFlowRecord.size()));
            } else {
                result.put("success", true);
                result.put("rows", new ArrayList<FlowRecord>());
            }
            return result;
        } catch (Exception e) {
            LCLogger.withException(e);
            result.put("success", false);
            result.put("rows", new ArrayList<FlowRecord>());
            result.put("result", e.getMessage());
        }
        return result;
    }

    /**
     * 分页查询业务对象审批历史
     */
    @RequestMapping(value = "/queryObjFlowRecordListPage")
    @ResponseBody
    public Map<String, Object> queryObjFlowRecordListPage(@JsonParam QueryParams qps, @JsonParam FlowRecord entity, HttpSession session,
                                                          HttpServletRequest request, HttpServletResponse response) {
        Map<String, Object> result = new HashMap<String, Object>(6);
        try {
            qps.preDealSecurity(request);
            FlowRecord t = (FlowRecord) BasicModel.transformClass(entity, qps);
            List<FlowRecord> list = flowRecordService.queryObjFlowRecordListPage(t);
            result.put("success", true);
            result.put("rows", list);
            this.pushCurrentDataCache(list, session.getId(), "base", false);
            result.put("total", getCount((BasicModel) t, list.size()));
        } catch (Exception e) {
            LCLogger.withException(e);
            result.put("success", false);
            result.put("rows", new ArrayList<FlowRecord>());
            result.put("result", e.getMessage());
        }
        return result;
    }

    /**
     * 根据审批流程id获取最新审批到哪条记录
     *
     * @author 姚江漪
     * @date 2019/07/01 10:07
     */
    @RequestMapping(value = "/queryLastNodeMaxSn")
    @ResponseBody
    public Map<String, Object> queryLastNodeMaxSn(@JsonParam FlowRecord entity, HttpSession session,
                                                  HttpServletRequest request, HttpServletResponse response) {
        Map<String, Object> result = new HashMap<String, Object>(4);
        try {
            FlowRecord flowRecord = flowRecordService.queryLastNodeMaxSn(entity);
            result.put("success", true);
            result.put("result", flowRecord);
        } catch (Exception e) {
            LCLogger.withException(e);
            result.put("success", false);
            result.put("result", e.getMessage());
        }
        return result;
    }
}
